package xyz.zhno.project.commons.web.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import xyz.zhno.project.commons.web.pojo.SwaggerProperties;

/**
 * Swagger 配置
 * @author zh_o
 * @date 2020-11-01
 */
@Configuration
@EnableSwagger2
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerConfig {

    @Autowired
    private SwaggerProperties properties;

    /**
     * 创建接口摘要信息
     * @return 摘要信息对象
     */
    @Bean
    public Docket createDocket() {
        // 创建摘要对象，指定文档类型
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getAaiInfo())
                .select()
                // 扫描包路径
                .apis(RequestHandlerSelectors.basePackage(properties.getBasePath()))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 获取 ApiInfo
     * @return ApiInfo 对象
     */
    private ApiInfo getAaiInfo() {
        // 构建 ApiInfo
        return new ApiInfoBuilder()
                // 标题
                .title(properties.getTitle())
                // 描述
                .description(properties.getDescription())
                // 作者信息
                .contact(new Contact(properties.getAuthor().getName(),
                        properties.getAuthor().getUrl(),
                        properties.getAuthor().getEmail()))
                // 版本号
                .version(properties.getVersion())
                .build();
    }

}
